﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFast.DB.Page;
using System.Web;
using System.Data;

namespace SFast.DB
{
    public static class Extensions
    {
        #region IEnumerable转换为Pagination类型，此类型包含了一些分页的相关信息
        /// <summary>
        /// IEnumerable转换为Pagination类型，此类型包含了一些分页的相关信息
        /// </summary>
        /// <typeparam name="T">集合对象 </typeparam>
        /// <param name="collection">集合</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示大小</param>
        /// <returns>返回Pagination集合</returns>
        public static Pagination<T> _ToPagination<T>(this IEnumerable<T> collection, int pageIndex, int pageSize)
        {
            return new Pagination<T>(collection, pageIndex, pageSize);
        }
        #endregion 

        #region IEnumerable转换为Pagination类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// <summary>
        /// IEnumerable 转换为Pagination类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// </summary>
        /// <typeparam name="T">集合对象 </typeparam>
        /// <param name="collection">集合</param>
        /// <param name="pageSize">每页显示大小</param>
        /// <returns>返回Pagination集合</returns>
        public static Pagination<T> _ToPagination<T>(this IEnumerable<T> collection, int pageSize)
        {
            int pageIndex = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null)
            {
                pageIndex = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"].Replace(",", "").Replace("&", "").Replace("?", ""));
            }
            return new Pagination<T>(collection, pageIndex, pageSize);
        }
        #endregion 

        #region DataTable 转换为Pagination 类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// <summary>
        /// DataTable 转换为Pagination 类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// </summary>
        /// <typeparam name="T">集合对象 </typeparam>
        /// <param name="dataTable">DataTable集合</param>
        /// <param name="pageSize">每页显示大小</param>
        /// <param name="rowCount">数据总行数，用于计算总页数</param>
        /// <returns>返回Pagination集合</returns>
        public static Pagination _ToPagination(this DataTable dataTable, int pageSize,int rowCount)
        {
            int pageIndex = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null)
            {
                pageIndex = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"].Replace(",", "").Replace("&", "").Replace("?", ""));
            }
            return new Pagination(dataTable, pageIndex, pageSize, rowCount);
        }
        #endregion 

        #region DataTable 转换为Pagination泛型类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// <summary>
        /// DataTable 转换为Pagination泛型类型，此类型包含了一些分页的相关信息，当前页码取自url中page参数值
        /// </summary>
        /// <typeparam name="T">集合对象 </typeparam>
        /// <param name="dataTable">DataTable集合</param>
        /// <param name="pageSize">每页显示大小</param>
        /// <param name="rowCount">数据总行数，用计算总页数</param>
        /// <returns>返回IPagination集合</returns>
        public static Pagination<T> _ToPagination<T>(this DataTable dataTable, int pageSize, int rowCount) where T:class,new()
        {
            int pageIndex = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null)
            {
                pageIndex = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"].Replace(",", "").Replace("&", "").Replace("?", ""));
            }
            return new Pagination<T>(dataTable._ToList<T>(), pageIndex, pageSize, rowCount);
        }
        #endregion 

        #region 生成分页代码，使用默认显示字符
        /// <summary>
        /// 生成分页代码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pag"></param>
        /// <returns></returns>
        public static String _ToPageHtml(this IPagination pag)
        {
            PagerOption p = new PagerOption();
            TencentPage ten = new TencentPage() { dataSource = pag, Option = p };
            return ten.Build();
        }
        #endregion

        #region 生成分页代码,传入显示语言字符
        /// <summary>
        /// 生成分页代码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pag"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static String _ToPageHtml(this IPagination pag,PagerOption p)
        {
            TencentPage ten = new TencentPage() { dataSource = pag, Option = p };
            return ten.Build();
        }
        #endregion 

    }
}
